package com.qg.lizhanqi.videosquare.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.qg.lizhanqi.videosquare.R;
import com.qg.lizhanqi.videosquare.uitlhelp.MyUtils;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.RequestMethod;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.RequestQueue;
import com.yolanda.nohttp.rest.Response;
import com.yolanda.nohttp.rest.SimpleResponseListener;

import org.json.JSONException;
import org.json.JSONObject;
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText editText_username;
    private EditText editText_user_phone;
    private EditText editText_passwrod;
    private EditText editText_confirm;
    private Button btn_submit;
    private RequestQueue requestQueues;
    final String url = "http://101.200.142.201:8080/VideoPlay/regist?";
    private String confirm;
    private String passwrod;
    private String username;
    private String userPhone;
    private static final String TAG = "李站旗注册页面" +
            "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        //第一步：创建Nohttp请求对列
        requestQueues = NoHttp.newRequestQueue();
        initView();
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_submit:
                if (submit()){      //校验信息
                    Log.i(TAG, "onClick: 可以注册");
                    requsetJson(username, userPhone, passwrod);//注册
                }
                break;
        }
    }
    private void requsetJson(String userName, String userPhone, String userPassword) {
        Log.i(TAG, "注册: "+username+userPhone+"密码:"+userPassword);

        Request<JSONObject> jsonObjectRequest = NoHttp.createJsonObjectRequest(url, RequestMethod.POST);
        jsonObjectRequest.add("userName",username);
        jsonObjectRequest.add("userPhone",userPhone);
        jsonObjectRequest.add("userPassword",userPassword);
        requestQueues.add(2, jsonObjectRequest, new SimpleResponseListener<JSONObject>() {
            @Override
            public void onSucceed(int i, Response<JSONObject> response) {
                JSONObject jsonObject = response.get();
                result(jsonObject.toString());
                Log.i(TAG, "onSucceed:Register的NOthttt的方法requsetJson请求成功 ");
            }
            @Override
            public void onFailed(int i, String s, Object o, Exception e, int i1, long l) {
                Toast.makeText(RegisterActivity.this, "onFailed：注册失败请稍后再试", Toast.LENGTH_SHORT).show();
            }
        });
    }
    public void result(String responseString){
        try {
            JSONObject responseJson = new JSONObject(responseString);
            String code = responseJson.getString("code");
            if(code.equals("00000")){
                String message = responseJson.getString("message");
                String userinfo = responseJson.getString("userinfo");
                Toast.makeText(RegisterActivity.this,message+ "请牢记账号密码"+userinfo, Toast.LENGTH_LONG).show();
                finish();
            }else if(code.equals("00010")){
                Toast.makeText(RegisterActivity.this, "注册失败！账号已存在", Toast.LENGTH_SHORT).show();
            }
            else if(code.equals("00020")){
                Toast.makeText(RegisterActivity.this, "注册失败！手机验证失败", Toast.LENGTH_SHORT).show();
            }
            else if(code.equals("00030")){
                Toast.makeText(RegisterActivity.this, "注册失败！请求方式错误", Toast.LENGTH_SHORT).show();
            }
            else if(code.equals("00040")){
                Toast.makeText(RegisterActivity.this, "注册失败！参数不能为空", Toast.LENGTH_SHORT).show();
            }
            else if(code.equals("00060")){
                Toast.makeText(RegisterActivity.this, "注册失败！Token过期", Toast.LENGTH_SHORT).show();
            }
            else if(code.equals("00070")){
                Toast.makeText(RegisterActivity.this, "注册失败！未知的错误", Toast.LENGTH_SHORT).show();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    private void initView() {
        editText_username = (EditText) findViewById(R.id.editText_username);
        editText_user_phone = (EditText) findViewById(R.id.editText_user_phone);
        editText_passwrod = (EditText) findViewById(R.id.editText_passwrod);
        editText_confirm = (EditText) findViewById(R.id.editText_confirm);
        btn_submit = (Button) findViewById(R.id.btn_submit);
        btn_submit.setOnClickListener(this);
    }
    @Override
    protected void onDestroy() {
        requestQueues.cancelAll();
        super.onDestroy();
    }
    /*校验信息*/
    private boolean submit() {
         username = editText_username.getText().toString().trim();
        if (TextUtils.isEmpty(username)) {
            Toast.makeText(this, "username不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }
        userPhone = editText_user_phone.getText().toString().trim();
        if (TextUtils.isEmpty(userPhone)) {
            Toast.makeText(this, "userPhone不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }
        passwrod = editText_passwrod.getText().toString().trim();
        if (TextUtils.isEmpty(passwrod)) {
            Toast.makeText(this, "passwrod不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }
        confirm = editText_confirm.getText().toString().trim();
        if (TextUtils.isEmpty(confirm)) {
            Toast.makeText(this, "confirm不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }

        if (!MyUtils.isuserPhoneNumber(userPhone)) {
            Toast.makeText(this, "手机号格式错误！请检查", Toast.LENGTH_SHORT).show();
        }

        if (!passwrod.equals(confirm)) {
            Toast.makeText(this, "两次密码不一致请重新设置密码", Toast.LENGTH_SHORT).show();
            editText_passwrod.setText("");
            editText_confirm.setText("");
            return false;
        }
        return true;
    }

}
